/**
 * Copyright (c) 2012, Hakans Pirates - 
 * Johan Andersson spaiki17@gmail.com, 
 * Mattias Lundberg lundberg.mattias@gmail.com, 
 * Samuel Sjodin sjoodin@gmail.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *  
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package pirates.inputDevices;

import com.jme3.app.SimpleApplication;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
import pirates.Settings;
import pirates.Settings.Directions;
import pirates.chat.ChatDisplay;
import pirates.chat.ChatState;
import pirates.ship.ShipControl;

/**
 * 
 * @author Johan
 */
public class KeyMouseControl extends AbstractControl implements ActionListener {

	private InputManager inputManager;
	private ShipControl shipControl;
	private ChatDisplay chat;
	private ChatState chatState = null;
	private SimpleApplication app;
	private boolean init = false;

	public KeyMouseControl(SimpleApplication app, InputManager inputManager,
			ChatDisplay chat) {

		this.inputManager = inputManager;
		this.chat = chat;
		this.app = app;
		setEnabled(true);
	}

	private void mapDirection(Directions[] directions) {
		for (Directions d : directions) {
			inputManager.addMapping(d.toString(), d.getKeyTrigger());
			inputManager.addListener(this, d.toString());
		}

	}

	@Override
	public void setSpatial(Spatial spatial) {
		this.spatial = spatial;
		if (spatial == null) {
			shipControl = null;
			return;
		}
		shipControl = spatial.getControl(ShipControl.class);
		if (shipControl == null) {
			throw new IllegalStateException("Spatial need ShipControl");
		}
	}

	@Override
	protected void controlUpdate(float tpf) {
	}

	@Override
	protected void controlRender(RenderManager rm, ViewPort vp) {
	}

	public Control cloneForSpatial(Spatial spatial) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void setEnabled(boolean enabled) {

		if (enabled && !init) {
			inputManager.addMapping("ShipJump", new KeyTrigger(
					KeyInput.KEY_RETURN));
			inputManager.addMapping("ShipAttackLeft", new MouseButtonTrigger(
					MouseInput.BUTTON_LEFT));
			inputManager.addMapping("ShipAttackRight", new MouseButtonTrigger(
					MouseInput.BUTTON_RIGHT));
			inputManager.addMapping("chat_command", new KeyTrigger(
					KeyInput.KEY_TAB));
			mapDirection(Settings.Directions.values());

			inputManager.addListener(this, "chat_command");
			inputManager.addListener(this, "ShipAttackLeft");
			inputManager.addListener(this, "ShipAttackRight");

			init = true;

		} else if (!enabled && init) {

			for (Directions d : Directions.values()) {
				inputManager.deleteMapping(d + "");
			}

			inputManager.deleteMapping("ShipAttackRight");
			inputManager.deleteMapping("ShipAttackLeft");
			inputManager.deleteMapping("ShipJump");
			inputManager.deleteMapping("chat_command");
			inputManager.removeListener(this);

			init = false;
		}

		super.setEnabled(enabled);
	}

	public void onAction(String name, boolean isPressed, float tpf) {

		if (name.equals("chat_command")) {
			if (isPressed) {
				setEnabled(false);
				if (chatState == null) {
					chatState = new ChatState(chat, this);
					chatState.initialize(app.getStateManager(), app);
					app.getStateManager().attach(chatState);
				}
				chatState.setEnabled(true);
			}
			return;
		}

		if (name.equals("ShipAttackLeft")) {
			if (isPressed) {
				shipControl.shoot(1);
			}
			return;
		}

		if (name.equals("ShipAttackRight")) {
			if (isPressed) {
				shipControl.shoot(-1);
			}
			return;
		}

		Directions d = Directions.valueOf(name);

		if (isPressed) {
			switch (d) {
				case LEFT :
					shipControl.steer(d.getValue(), tpf);
					break;
				case RIGHT :
					shipControl.steer(d.getValue(), tpf);
					break;
				case FORWARD :
					shipControl.accelerate(d.getValue(), tpf);
					break;
				case BACKWARDS :
					shipControl.accelerate(d.getValue(), tpf);
					break;
				case UP :
					shipControl.aim(d.getValue(), tpf);
					break;
				case DOWN :
					shipControl.aim(d.getValue(), tpf);
					break;
				case DROP :
					shipControl.drop();
					break;
			}
		} else {
			switch (d) {
				case LEFT :
					shipControl.steer(0, tpf);
					break;
				case RIGHT :
					shipControl.steer(0, tpf);
					break;
				case FORWARD :
					shipControl.accelerate(0, tpf);
					break;
				case BACKWARDS :
					shipControl.accelerate(0, tpf);
					break;
				case UP :
					shipControl.aim(0, tpf);
					break;
				case DOWN :
					shipControl.aim(0, tpf);
					break;
			}
		}
	}
}
